package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.DeptMapper;
import com.example.demo.pojo.Dept;
import com.example.demo.pojo.Result;
import com.example.demo.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * @Description: DeptServiceImpl
 * </p>
 * @Author yangjiwang
 * @Date 2023/2/27
 */
@Service("deptService")
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Result<Dept> createOrUpdate(Dept entity) {
        //根据名称查询数据库中是否存在该部门
        Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>().eq(Dept::getDeptName, entity.getDeptName()));

        if (dept != null) {
            //存在，添加失败
            return Result.error("部门已存在，新增失败");
        }

        //插入
        boolean flag = super.saveOrUpdate(entity);
        if (!flag) {
            return Result.error("新增操作失败");
        }

        return Result.ok("新增部门信息成功");
    }
}
